﻿Imports System.DirectoryServices.ActiveDirectory

''' <summary>
''' ドメイン関連情報画面のフォームです。
''' </summary>
''' <remarks></remarks>
Friend NotInheritable Class DomainInfo

#Region " プライベートフィールド "
  Private ReadOnly adDomain As Domain
#End Region

#Region " パブリックコンストラクター "
  ''' <summary>
  ''' ドメインを指定して DomainInfo クラスの新しいインスタンスを初期化します。
  ''' </summary>
  ''' <param name="domain">ドメイン。</param>
  ''' <exception cref="ArgumentNullException"><paramref name="domain"/> が Nothing です。</exception>
  ''' <remarks></remarks>
  Public Sub New(domain As Domain)
    If domain Is Nothing Then
      Throw New ArgumentNullException("domain", "domain が Nothing です。")
    End If

    ' この呼び出しはデザイナーで必要です。
    InitializeComponent()
    ' InitializeComponent() 呼び出しの後で初期化を追加します。

    adDomain = domain
    Me.ShowInfo()   'ドメイン関連情報を表示
  End Sub
#End Region

#Region " イベントハンドラ "
  Private Sub GCListBox_SelectedIndexChanged(
    sender As Object, e As EventArgs) Handles GCListBox.SelectedIndexChanged

    Me.GCSiteLabel.Text = DirectCast(Me.GCListBox.SelectedItem, GlobalCatalog).SiteName
  End Sub

  Private Sub SiteListBox_SelectedIndexChanged(
    sender As Object, e As EventArgs) Handles SiteListBox.SelectedIndexChanged

    With DirectCast(Me.SiteListBox.SelectedItem, ActiveDirectorySite)
      Me.ServerListBox.DataSource = .Servers.Cast(Of DirectoryServer).ToList()
      Me.SiteLinkListBox.DataSource = .SiteLinks.Cast(Of ActiveDirectorySiteLink).ToList()
      Me.SubnetListBox.DataSource = .Subnets
    End With
  End Sub

  Private Sub SiteLinkListBox_SelectedIndexChanged(
    sender As Object, e As EventArgs) Handles SiteLinkListBox.SelectedIndexChanged

    With DirectCast(Me.SiteLinkListBox.SelectedItem, ActiveDirectorySiteLink)
      Me.CostLabel.Text = .Cost.ToString()
      Me.RepliLabel.Text = .ReplicationInterval.ToString()
      Me.TransportLabel.Text = .TransportType.ToString()
    End With
  End Sub

  Private Sub DCListBox_SelectedIndexChanged(
    sender As Object, e As EventArgs) Handles DCListBox.SelectedIndexChanged

    With adDomain.DomainControllers.Item(Me.DCListBox.SelectedIndex)
      Me.OSLabel.Text = .OSVersion
      Me.IPLabel.Text = .IPAddress
      Me.DCSiteLabel.Text = .SiteName
    End With
  End Sub
#End Region

#Region " プライベートメソッド "
  ''' <summary>
  ''' ドメイン関連情報を表示します。
  ''' </summary>
  ''' <remarks></remarks>
  Private Sub ShowInfo()
    With adDomain
      With .Forest
        Me.ForestLabel.Text = .Name   'フォレスト
        Me.ForestModeLabel.Text = .ForestMode.ToString()
        Me.RootDomainLabel.Text = .RootDomain.Name
        For Each gc As GlobalCatalog In .GlobalCatalogs
          Me.GCListBox.Items.Add(gc)
        Next
        Me.GCListBox.SelectedIndex = 0
        For Each domain As Domain In .Domains
          Me.DomainListBox.Items.Add(domain)
        Next

        For Each site As ActiveDirectorySite In .Sites    'サイト
          Me.SiteListBox.Items.Add(site)
        Next
        Me.SiteListBox.SelectedIndex = 0
      End With

      Me.DomainLabel.Text = .Name   'ドメイン
      Me.DomainModeLabel.Text = .DomainMode.ToString()
      For Each dc As DomainController In .DomainControllers
        Me.DCListBox.Items.Add(dc.Name.Substring(0, dc.Name.IndexOf("."c)))
      Next
      Me.DCListBox.SelectedIndex = 0

      Me.SchemaLabel.Text = .Forest.SchemaRoleOwner.Name    'FSMO
      Me.NamingLabel.Text = .Forest.NamingRoleOwner.Name
      Me.RIDLabel.Text = .RidRoleOwner.Name
      Me.PDCLabel.Text = .PdcRoleOwner.Name
      Me.InfraLabel.Text = .InfrastructureRoleOwner.Name
    End With
  End Sub
#End Region
End Class